package com.sgb.kjwl.view.ui.activity.self;

import android.content.Context;
import android.content.Intent;

import androidx.databinding.DataBindingUtil;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.bumptech.glide.Glide;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityEnterpriseInfoBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.model.entity.businessmanagement.CompanyEntity;
import com.sgb.kjwl.viewmodel.self.EnterpriseInfoModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.DateUtils;
import com.swgk.core.util.SharedPreferenceUtil;

import javax.inject.Inject;

/*
个人中心的企业信息界面
 */
public class EnterpriseInformationActivity extends AppActivity implements View.OnClickListener {
    @Inject
    EnterpriseInfoModel viewModel;
    private ActivityEnterpriseInfoBinding binding;
    private CompanyEntity mCompanyEntity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    //跳转传值
    public static void start(Context context, CompanyEntity companyEntity) {
        //创建intent
        Intent intent = new Intent();
        intent.setClass(context, EnterpriseInformationActivity.class);
        intent.putExtra(AppConstant.EXTRA_OBJ, companyEntity);
        context.startActivity(intent);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }


    @Override
    protected void initView() {
        //intent传参
        mCompanyEntity = (CompanyEntity) getIntent().getSerializableExtra(AppConstant.EXTRA_OBJ);
        //双向绑定view和activity
        binding = DataBindingUtil.setContentView(EnterpriseInformationActivity.this, R.layout.activity_enterprise_info);
    }

    @Override
    protected void init() {
        //设置头部信息
        binding.include.titleView.setText("组织信息");
        binding.include.rightView.setText("    ");
        //设置点击事件
        binding.include.ivBack.setOnClickListener(this);
        //判断传过来的值是否存在
       /* if (mCompanyEntity != null) {
            //存在就网络请求获取我的公司相关信息
            viewModel.getMyInfoByCompanyNo(mCompanyEntity.getCompNo());
        }*/
        showMyInfoByCompanyNo(mCompanyEntity);
    }

    //设置状态栏为暗
    @Override
    protected boolean statusBarDark() {
        return true;
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder().appComponent(appComponent).selfModule(new SelfModule(this)).build().inject(this);
    }

    //网络请求返回：按公司显示我的信息
    public void showMyInfoByCompanyNo(CompanyEntity data) {
        //判断当前公司是否存在logo头像
        if (!TextUtils.isEmpty(data.getCompLogo())) {
            //如果存在就展示公司logo头像
            Glide.with(this).load(data.getCompLogo()).into(binding.corporatePortrait);
        } else {
            //如果没有就展示公司默认logo头像
            switch (data.getType()){
                case 1:
                    binding.corporatePortrait.setImageResource(R.mipmap.icon_company);
                    binding.enterpriseAdministration.setText("企业认证");
                    break;
                case 2:
                    binding.corporatePortrait.setImageResource(R.drawable.icon_team_default);
                    binding.enterpriseAdministration.setText("团队认证");
                    break;
                default:
                    binding.corporatePortrait.setImageResource(R.drawable.icon_organization_default);
                    binding.enterpriseAdministration.setText("政府认证");
                    break;
            }

        }
        //设置企业名字
        binding.enterpriseName.setText(data.getCompName());
        //设置我的名字
        String realName = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.REAL_NAME, "");
        binding.tvMyName.setText(realName);
        //设置我的电话
        String phone = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LOGIN_PHONE, "");
        binding.tvUserPhone.setText(phone);
        //设置主管理员
//        binding.enterpriseAdministration.setText("主管理员:" + data.getAdministrators());
        //设置部门
        if (!TextUtils.isEmpty(data.getDepartName()) && !TextUtils.isEmpty(data.getRoleName())) {
            binding.tvUserDepartment.setText(data.getDepartName() + "-" + data.getRoleName());
        } else if (TextUtils.isEmpty(data.getDepartName()) && !TextUtils.isEmpty(data.getRoleName())) {
            binding.tvUserDepartment.setText(data.getRoleName());
        } else if (!TextUtils.isEmpty(data.getDepartName()) && TextUtils.isEmpty(data.getRoleName())) {
            binding.tvUserDepartment.setText(data.getDepartName());
        }

        //设置职位
        binding.tvUserPosition.setText(data.getRoleName());
        //认证时间
        binding.tvAuthTime.setText(DateUtils.timestampTo13DateNos(data.getAuthTime()) + "入驻");
    }

    //点击事件
    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            //点击关闭该界面
            case R.id.iv_back: {
                finish();
            }
        }
    }
}
